<?php
if (!defined("MCTDESIGN")) die("#");
$w = isset($_GET["w"]) ? $_GET["w"] : "banner";

if (isset($_POST['delete'])) {
	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;
 	for($i=1;$i<=$total;$i++){
		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';
		if (!empty($checkbox[$i])) {
			$mysql->query("DELETE FROM ".$tb_fix."_logos WHERE logo_id='".$checkbox[$i]."'");
		}
	}
	$html_body = _html_trans("Đã xoá các logo thành công !");
	return false;
}
if (isset($_POST['order'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

    for ($i=1;$i<=$total;$i++)

      {

           $lg_id    = isset($_POST["order_id_$i"])    ? $_POST["order_id_$i"]    : 0;

           $lg_order = isset($_POST["order_$i"]) ? $_POST["order_$i"] : 0;

           if ( ($lg_id>0)&&($lg_order>0) )

           {		

				$mysql->query("UPDATE ".$tb_fix."_logos SET logo_order='$lg_order' WHERE logo_id='".$lg_id."'");

           }

    }

	$html_body = _html_trans("Đã cập nhật thứ tự các logo thành công !");
	return false;

}
function del_img($id){
    global $tb_fix, $anyDB;
     $sql = "select logo_img from ".$tb_fix."_logos where logo_id = $id";
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $link_img = $r['logo_img'];
        unlink($link_img);    
}
if (isset($_POST['update'])) {
	$lg_name = isset($_POST["lg_name"]) ? stripslashes($_POST["lg_name"]) : "";
	$lg_position = isset($_POST["lg_position"]) ? stripslashes($_POST["lg_position"]) : "";
	$lg_url = isset($_POST["lg_url"]) ? stripslashes($_POST["lg_url"]) : "";
    $choise = $_POST['choise_logo'];
	
	if (empty($lg_name) || empty($lg_position) || empty($lg_url)) 
	{
		$html_body = _html_trans("Bạn phải nhập đầy đủ thông tin !");
		return false;
	}
    
    //
	if($choise == 1){// chon upload
            if(empty($_FILES['lg_img']['name'])){
                $html_body = _html_trans("Bạn chưa chọn logo để upload! ");
		        return false;
            }else{
                $lg_img = isset($_FILES['lg_img']['name'] ) ? $_FILES['lg_img']['name'] : '';
	            $lg_tmp_img = isset($_FILES['lg_img']['tmp_name'] ) ? $_FILES['lg_img']['tmp_name'] : '';   
                $lg_img = uploadIMG($lg_img,$lg_tmp_img,"logos"); 
                $sql_img = ", logo_img='$lg_img' ";
                del_img($id);
            }     
     }else if($choise == 2){ // chon link
            $lg_img = $_POST['lg_img_link'];
            $sql_img = ", logo_img='$lg_img' ";
            del_img($id);
     }else{
        $sql_img = "";
     }
     $sql = "UPDATE ".$tb_fix."_logos SET logo_name='".$lg_name."',logo_url='$lg_url',logo_where='$lg_position' $sql_img WHERE logo_id='".$id."'";  
    $mysql->query($sql);
    
	$html_body = _html_trans("Đã cập nhật logo mới thành công !","admin.php?obj=Logos");
	return false;
}
if (isset($_POST['add'])) {
	$lg_name = isset($_POST["lg_name"]) ? stripslashes($_POST["lg_name"]) : "";
	$lg_position = isset($_POST["lg_position"]) ?stripslashes($_POST["lg_position"]) : "";
	$lg_url = isset($_POST["lg_url"]) ? stripslashes($_POST["lg_url"]) : "";
    $choise = $_POST['choise_logo'];
	if (empty($lg_name) || empty($lg_position) || empty($lg_url) || empty($choise)) 
	{
		$html_body = _html_trans("Bạn phải nhập đầy đủ thông tin !");
		return false;
	}
    if($choise == 1){// chon upload
            if(empty($_FILES['lg_img']['name'])){
                $html_body = _html_trans("Bạn chưa chọn logo để upload! ");
		        return false;
            }else{
                $lg_img = isset($_FILES['lg_img']['name'] ) ? $_FILES['lg_img']['name'] : '';
	            $lg_tmp_img = isset($_FILES['lg_img']['tmp_name'] ) ? $_FILES['lg_img']['tmp_name'] : '';   
                $lg_img = uploadIMG($lg_img,$lg_tmp_img,"logos"); 
            }     
     }else if($choise == 2){ // chon link
            $lg_img = $_POST['lg_img_link'];
        }    
	$q = $anyDB->query("SELECT MAX(logo_order) AS max_order FROM ".$tb_fix."_logos WHERE logo_where = '$lg_position'");
	$r = $anyDB->getNext($q);
	$order = $r['max_order']+1;
	$mysql->query("INSERT INTO ".$tb_fix."_logos (logo_name,logo_url,logo_img,logo_where,logo_order) VALUES ('$lg_name','$lg_url','$lg_img','$lg_position','$order')");
	$html_body = _html_trans("Đã thêm logo mới thành công !");
	return false;
}
function list_position($p='') 
{
	$qc_vitri = array(
        "trai" => "Bên trái",
        "giua" => "Giữa",
        "phai" => "Bên phải",
        "duoi" => "Bên dưới"
    );
    return $qc_vitri;
}
function _html_editLogos($_id)
{
	global $dir_tpl, $anyDB, $tb_fix,$temp;
	$q = $anyDB->query("SELECT * FROM ".$tb_fix."_logos WHERE logo_id='$_id' LIMIT 1");
	$t = $anyDB->numRows($q);
	if ($t>0) {
		$r = $anyDB->getNext($q);
		$temp_body = $temp;
		$list_position = list_position();
		$temp_body->assign( 'list_position', $list_position );
		/*$temp_body->assign( 'lg_name', "".$r["logo_name"]."" );
		$temp_body->assign( 'lg_url', "".$r["logo_url"]."" );
		$temp_body->assign( 'lg_img', "<img width=\"120px\" src=\"".$r["logo_img"]."\">" );*/
        $temp_body->assign( 'logo', $r );
        $temp_body->assign("breakcum",breakcum("Quản lý quảng cáo  "));
		$_html = $temp_body->fetch( '_adm_logos_edit.mct' );
	}
	else $_html = _html_trans("Không tồn tại logo này trong database !","admin.php?obj=Logos");
	return $_html;
}
function _html_manageLogos() 
{
	global $dir_tpl, $anyDB, $tb_fix,$temp;
	$temp_body = $temp;
	$temp_body->assign( 'list_position', list_position() );
    $where = "";
    if(isset($_GET['where'])){
        $w = fixquotes($_GET['where']);
        if($w == 'all'){
            $where = " ";    
        }else{
            $where = " WHERE logo_where = '$w' and logo_banner =0 ";    
        }
    }    
	$q = $anyDB->query("SELECT * FROM ".$tb_fix."_logos where logo_banner =0 ORDER BY logo_order ASC");    
	$_c = 0;
	$t = $anyDB->numRows($q);
    if($t > 0){
        $list_logo = $anyDB->getAll($q);
        foreach($list_logo as $k=>$v){
            $logo_where = $v['logo_where'];
            $list_position = list_position();
            foreach($list_position as $key => $value){
                if($logo_where == $key){
                    $list_logo[$k]['logo_where'] = $value;
                }
            }
        }
    }
    
    
	$temp_body->assign( 'total', $t );
    $temp_body->assign( 'w', $w );
    //echo "<pre>";print_r($list_logo);
    //get su kien 
   	$qr = $anyDB->query("SELECT * FROM ".$tb_fix."_logos where logo_banner =1  "); 
    $sk = $anyDB->getNext($qr);
    $temp_body->assign( 'sk',$sk);
	$temp_body->assign( 'list_logo',$list_logo);
    $temp_body->assign( 'dir_tpl', "".$dir_tpl."" );
    $temp_body->assign("breakcum",breakcum("Quản lý quảng cáo  "));
	$_html = $temp_body->fetch( '_adm_logos.mct' );
	return $_html;
}
if ($act == 'editsukien'){
    global $dir_tpl, $anyDB, $tb_fix,$temp,$mysql;
    $id = $_POST['idsukien'];
    $on = $_POST['sudung'];
    
    if ($on == 'on'){
        $sudung = 1;
     }else{
        $sudung = 0;
    }
    
    //echo $sudung ; die();
	$lg_name = isset($_POST["lg_name"]) ? stripslashes($_POST["lg_name"]) : "";
	$lg_url = isset($_POST["lg_url"]) ? stripslashes($_POST["lg_url"]) : "";
	if (empty($lg_name)  || empty($lg_url)) 
	{
		$html_body = _html_trans("Bạn phải nhập đầy đủ thông tin !");
		return false;
	}else if(!empty($_FILES['lg_img']['name'])){
        @unlink($_POST['hinhcu']);       
        $lg_img = isset($_FILES['lg_img']['name'] ) ? $_FILES['lg_img']['name'] : '';
        $lg_tmp_img = isset($_FILES['lg_img']['tmp_name'] ) ? $_FILES['lg_img']['tmp_name'] : '';   
        $lg_img = uploadIMG($lg_img,$lg_tmp_img,"logos"); 
        $sql_img = ", logo_img='$lg_img' ";               
    }
    $sql = "UPDATE ".$tb_fix."_logos SET logo_name ='".$lg_name."',logo_status = $sudung,logo_url ='$lg_url'  $sql_img  WHERE logo_id ='".$id."' ";
    //echo $sql ;die();
    $mysql->query($sql);
    
	$html_body = _html_trans("Đã cập nhật sự kiện thành công !","admin.php?obj=Logos#tabs-2");
	return false;    
        
    
}


if ($act=="edit") {
	$html_body = _html_editLogos($id);
	return false;
}

$html_body = _html_manageLogos();
/**
 * McT Design - mongcaitoday.com
 * Zai Dung - hd_n2ct@yahoo.com
 * thong tin: quan ly logos
 * hoan thanh: 1h:09 24/03/2011
 */
?>